home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / portablc.zip / CHGXMITF.C next >
Text File  |  1989-12-13  |  7KB  |  253 lines

  1. /*************************************************************************
  2.         chgxmitf.c  : Creates/Uncreates transmittable ASCII files
  3.                       from either ASCII or BINARY files
  4.  
  5. I personally put this together for use with Kermit since it may change bin-
  6. ary files while sending or receiving them. However, anyone can use this
  7. to safely transmit any type of file as long as you have this routine
  8. on the other end to decode the file back to the original position.
  9. This works on the following systems: Unix Version 5.0 on both Motorola
  10. and Xenix, and DOS compiled under Microsoft Version 5.1.
  11.  
  12.         12/8/89 Martin Katz
  13.  
  14. *************************************************************************/
  15.  
  16. #include  <stdio.h>
  17.  
  18. #define ddig(c) ((c) >= 0x0 && (c) <= 0x9)
  19. #define hdig(c) ((c) >= 0xa && (c) <= 0xf)
  20. #define MASK    0x0f
  21.  
  22. #define dchr(c) ((c) >= '0' && (c) <= '9')
  23. #define hchr(c) ((c) >= 'A' && (c) <= 'F')
  24.  
  25. #ifdef TWO60
  26. #define CLEAR  printf("\033X")
  27. #else
  28. #define CLEAR  printf("\033[H\033[2J\n")
  29. #endif
  30.  
  31. #define BELL printf("\007\n")
  32.  
  33. extern  int errno;
  34.  
  35. FILE *infile, *outfile;                         /* input & output files */
  36.  
  37. unsigned long int textsize = 0,   /* text size counter */
  38.                                   codesize = 0,   /* code size counter */
  39.                 printcount = 0; /* counter reporting progress every 1K bytes*/
  40.  
  41.  
  42. char *itohex(c,s)
  43. register unsigned char c;
  44. register char *s;
  45. {
  46. register char c1, c2;
  47.  
  48. #ifdef DEBUG
  49.         fprintf(stderr,"In itohex()...\n");
  50.         fflush(stderr);
  51. #endif
  52.  
  53.     c1 = (c >> 4) & MASK;
  54.     c2 = c & MASK;
  55.  
  56.         s[0] = ddig(c1) ? c1 + '0' : (hdig(c1) ? c1 - 0xa + 'A' : c1);
  57.         s[1] = ddig(c2) ? c2 + '0' : (hdig(c2) ? c2 - 0xa + 'A' : c2);
  58.         s[2] = '\0';
  59.  
  60.         return(s);
  61. }
  62.  
  63. char hextoi(c1,c2)
  64. register        char    c1, c2;
  65. {
  66. register        char    i;
  67.  
  68. #ifdef DEBUG
  69.         fprintf(stderr,"In hextoi()...\n");
  70.         fflush(stderr);
  71. #endif
  72.  
  73.         i = dchr(c1) ? c1 - '0' : (hchr(c1) ? c1 - 'A' + 0xa : c1);
  74.         return (i << 4) + (dchr(c2) ? c2 - '0' : (hchr(c2) ? c2 - 'A' + 0xa : c2));
  75. }
  76.  
  77. void Decode()       /* Just the reverse of Encode(). */
  78. {
  79.         unsigned long int len = 0;
  80.         char in_buf[5];
  81.         char    hextoi();
  82.  
  83. #ifdef DEBUG
  84.         fprintf(stderr,"In Decode()...\n");
  85.         fflush(stderr);
  86. #endif
  87.  
  88.         while ( fgets(in_buf,2,infile) != NULL )
  89.         {
  90.  
  91.                 if ( fgets(&in_buf[1],2,infile) == NULL )
  92.                         break;
  93.  
  94.                 len+=2;
  95.  
  96.                 textsize = len;
  97.  
  98.                 putc(hextoi(in_buf[0],in_buf[1]),outfile);
  99.  
  100.                 codesize++;
  101.                 printcount++;
  102.  
  103.                 if (printcount > (unsigned long) 1024)
  104.                 {
  105.                         printf("%12ld\r", textsize);
  106.                         printcount = 0;
  107.                         /* Reports progress each time the textsize exceeds
  108.                            multiples of 1024. */
  109.                 }
  110.         }
  111.  
  112.         printf("In : %ld bytes\n", textsize);   /* Encoding is done. */
  113.         printf("Out: %ld bytes\n", codesize);
  114.         printf("Out/In: %.3f\n", (double)codesize / textsize);
  115. }
  116.  
  117. void Encode()
  118. {
  119.         unsigned long int in_cnt, read_len;
  120.         char in_buf[1050], to_buf[5];
  121.         char *itohex();
  122.  
  123. #ifdef DEBUG
  124.         fprintf(stderr,"In Encode()...\n");
  125.         fflush(stderr);
  126. #endif
  127.  
  128.         while ( (read_len = fread(in_buf,1,1024,infile)) != 0 )
  129.         {
  130.                 in_cnt = 0;
  131.                 while ( in_cnt < read_len )
  132.                 {
  133.                         fwrite (itohex(in_buf[in_cnt++],to_buf), 1, 2, outfile);
  134.                         textsize++;
  135.                         codesize+=2;
  136.                         printcount++;
  137.                         if (printcount > (unsigned long) 1024)
  138.                         {
  139.                                 printf("%12ld\r", textsize);
  140.                                 printcount = 0;
  141.                                 /* Reports progress each time the textsize exceeds
  142.                                    multiples of 1024. */
  143.                         }
  144.                 }
  145.         }
  146.  
  147.         putc ('\n', outfile);
  148.  
  149.         printf("In : %ld bytes\n", textsize);   /* Encoding is done. */
  150.         printf("Out: %ld bytes\n", codesize);
  151.         printf("Out/In: %.3f\n", (double)codesize / textsize);
  152.  
  153. }
  154.  
  155. void usage()
  156.  
  157. {
  158.         CLEAR;
  159.         printf("chgxmitf: Four Arguments are required as exemplified:\n");
  160.         printf("'chgxmitf c file1 file2' turns file1 into transmittable file2.\n");
  161.         printf("'chgxmitf d file2 file1' turns transmittable file2 into actual file1.\n");
  162.         BELL;
  163. }
  164.  
  165. arg_check(argc, argv)
  166. int  argc;
  167. char *argv[];
  168. {
  169.         if (argc != 4)
  170.         {
  171.                 usage();
  172.         return(-1);
  173.         }
  174.  
  175.     if ( strpbrk(argv[1], "DCdc") == NULL )
  176.         {
  177.                 usage();
  178.         return(-1);
  179.         }
  180.  
  181.         if ( (strcmp(argv[3],argv[2]) == 0)  &&  (strpbrk(argv[1], "Cc") != NULL) )
  182.         {
  183.                 CLEAR;
  184.                 printf("chg_xmitfile: Cannot overlay a file with transmittable version.\n");
  185.                 BELL;
  186.         return(-1);
  187.         }
  188.  
  189.         return(0);
  190.  
  191. }
  192.  
  193. main(argc, argv)
  194. int  argc;
  195. char *argv[];
  196. {
  197.         char  *to_file;
  198.         char  cmnd[100], real_to_file_name[15];
  199.         short overlay_file = 0;
  200.  
  201.         if (arg_check(argc, argv))
  202.                 exit(-1);
  203.  
  204.         if ( strcmp(argv[3],argv[2]) == 0 )
  205.         {
  206.                 overlay_file = 1;
  207.                 to_file = "interim_file";
  208.                 strcpy(real_to_file_name,argv[3]);
  209.         }
  210.         else
  211.                 to_file = argv[3];
  212.  
  213.  
  214.         if (( infile  = fopen(argv[2], "r+b")) == NULL )
  215.         {
  216.                 CLEAR;
  217.                 printf("chg_xmitfile: unable to open from file %s\n", argv[2]);
  218.                 BELL;
  219.                 return(-1);
  220.         }
  221.  
  222.     if (( outfile = fopen(to_file, "w+b")) == NULL )
  223.         {
  224.                 CLEAR;
  225.                 printf("chg_xmitfile: unable to open to file %s\n", to_file);
  226.                 BELL;
  227.                 return(-1);
  228.         }
  229.  
  230.     if (toupper(*argv[1]) == 'C')
  231.         {
  232.                 printf("\n-->Changing %s into transmittable %s<--\n", argv[2], to_file);
  233.                 Encode();
  234.         }
  235.         else
  236.         {
  237.                 printf("\n-->Creating actual file %s<--\n", to_file);
  238.                 Decode();
  239.         }
  240.  
  241.         fclose(infile);
  242.         fclose(outfile);
  243.  
  244.         if ( overlay_file == 1 )
  245.         {
  246.                 sprintf(cmnd,"copy interim_file %s", real_to_file_name);
  247.                 system(cmnd);
  248.                 system("del interim_file");
  249.         }
  250.  
  251.         return(0);
  252. }
  253.